package com.example.myapplication;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication.audio.AudioListActivity;
import com.example.myapplication.databinding.ActivityMainBinding;
import com.example.myapplication.utils.Contains;
import com.example.myapplication.utils.FileInterface;
import com.example.myapplication.utils.OnPermissionCallbackListener;
import com.example.myapplication.utils.PermissionUtils;
import com.example.myapplication.utils.StorageUtils;

import java.io.File;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    String[] permissions = {
            Manifest.permission.RECORD_AUDIO,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };
    private ActivityMainBinding binding;
    private final int time = 3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.mainText.setText("正在获取权限，请稍等——");
        PermissionUtils instance = PermissionUtils.getInstance();

        OnPermissionCallbackListener listener = new OnPermissionCallbackListener() {
            @Override
            public void onGranted() {
                //进入页面，创建应用文件夹
                createAppDir();
                System.out.println("应用文件夹创建成功！");
                //redirect to main page
                startActivity(new Intent(MainActivity.this, AudioListActivity.class));
            }

            @Override
            public void onDenied(List<String> deniedPermissions) {
                PermissionUtils.getInstance().showDialogUserGotoSetting(MainActivity.this);
            }
        };

        instance.requestPermission(this, permissions, listener);
    }



    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionUtils.getInstance().onRequestPermissionsResult(this, requestCode, permissions, grantResults);
    }

    public void createAppDir() {
        File recordDir = StorageUtils.getInstance().createAppSubdirectory(FileInterface.SUB_DIR_AUDIO);
        //设置公共变量
        Contains.PATH_SUB_RECORD = recordDir.getAbsolutePath();
        System.out.println("父目录："+recordDir.getParent());
        System.out.println("保存子目录："+recordDir.getAbsolutePath());
        Contains.PATH_APP_DIR = recordDir.getParent();
    }
}